package com.nf.health.app.activity.my.historydata;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.github.jdsjlzx.util.RecyclerViewStateUtils;
import com.github.jdsjlzx.view.LoadingFooter;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.my.adapter.QuerySweatTrackListAdapter;
import com.nf.health.app.activity.my.bean.SweatQuestryModel;
import com.nf.health.app.customview.DividerListItemDecoration;
import com.nf.health.app.http.Act;
import com.nf.health.app.models.Family;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ViewUtil;
import com.nf.health.app.widget.MyMainItemAnimation;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 修改家庭责任人的历史查询数据
 */

public class SweatQueryHistoryActivity2 extends BaseActivity {
    @Bind(R.id.rcl_ecg_mian_history)
    LRecyclerView rcl_ecg_mian_history;
    @Bind(R.id.frl_leftview)
    FrameLayout frl_leftview;
    private int  pageNo = 1 ;
    private int pageSize = 20;
    private String deviceType;
    private String touserid;
    private List<SweatQuestryModel> list;
    public boolean isOpen =false;//开关默认是关闭的
    private boolean isCanLoadmore = false;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private QuerySweatTrackListAdapter querySweatTrackListAdapter;
    private int delIndex = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_ecgqueryhistory);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "历史记录");



       ViewUtil.setText(this, R.id.titlebar_title_tv, "历史记录");
        rcl_ecg_mian_history.setItemAnimator(new MyMainItemAnimation());
        rcl_ecg_mian_history.setLayoutManager(new LinearLayoutManager(this));
        rcl_ecg_mian_history.addItemDecoration(new DividerListItemDecoration(SweatQueryHistoryActivity2.this, LinearLayoutManager.VERTICAL, 20,
                getResources().getColor(R.color.divider_line_color)));
        rcl_ecg_mian_history.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        rcl_ecg_mian_history.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
         rcl_ecg_mian_history.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                RecyclerViewStateUtils.setFooterViewState(rcl_ecg_mian_history, LoadingFooter.State.Normal);
                pageNo  = 1;
                loadData(Act.Flag.FLAG_REFRESH_DATA);
            }
        });
        rcl_ecg_mian_history.setPullRefreshEnabled(false);
        rcl_ecg_mian_history.setOnLoadMoreListener(null);
         /*rcl_ecg_mian_history.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(rcl_ecg_mian_history);
                if(state == LoadingFooter.State.Loading) {
                    return;
                }
                if(isCanLoadmore){
                    pageNo ++;
                    loadData(Act.Flag.FLAG_LOAD_MODE_DATA);
                    RecyclerViewStateUtils.setFooterViewState(EcgQueryHIstoryActivity.this, rcl_ecg_mian_history, pageSize, LoadingFooter.State.Loading, null);
                }else{
                    RecyclerViewStateUtils.setFooterViewState(EcgQueryHIstoryActivity.this, rcl_ecg_mian_history, pageSize, LoadingFooter.State.TheEnd, null);
                }
            }
        });*/
        list = new ArrayList<>();
        String deviceName = getIntent().getStringExtra("deviceName");
        deviceType = getIntent().getStringExtra("deviceType");
        Family family = (Family) getIntent().getSerializableExtra("member");
        if(TextUtils.isEmpty(family.getTouserid())){
            touserid = "";
        }else{
            touserid = family.getTouserid();
        }
        loadData(Act.Flag.FLAG_REFRESH_DATA);
    }
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        switch (actionName){
            case  Act.Flag.FLAG_REFRESH_DATA://下拉刷新列表
                list.clear();
                try {
                    JSONObject  jsonObject = new JSONObject(object.toString());
                    String deviceDffata = jsonObject.optString("deviceDesc");
                    JSONArray deviceDataArray = jsonObject.optJSONArray("deviceData");
                    if(deviceDataArray == null || deviceDataArray.length() == 0){
                        return ;
                    }
                    JSONArray jsonTimeArray = deviceDataArray.getJSONObject(0).optJSONArray("xValue");
                    for (int j = 0; j < jsonTimeArray.length(); j++) {
                        SweatQuestryModel model = new SweatQuestryModel();
                        List<String> subList = new ArrayList<>();
                        for (int i = 0; i < deviceDataArray.length(); i++) {
                            JSONArray jsonTempDataArray = deviceDataArray.getJSONObject(i).optJSONArray("yValue");
                            String result = jsonTempDataArray.optString(j);
                            subList.add(result);
                        }
                        model.setList(subList);
                        model.setTime(jsonTimeArray.optString(j));
                        list.add(model);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if(list.size() != 0){
                    frl_leftview.setVisibility(View.VISIBLE);
                    if(querySweatTrackListAdapter == null){
                        querySweatTrackListAdapter = new QuerySweatTrackListAdapter(SweatQueryHistoryActivity2.this,list);
                        mLRecyclerViewAdapter = new LRecyclerViewAdapter(querySweatTrackListAdapter);
                        rcl_ecg_mian_history.setAdapter(mLRecyclerViewAdapter);
                    }else{
                        mLRecyclerViewAdapter.notifyDataSetChanged();
                    }
                    if(list.size() < pageSize){//说明加载完毕
                        rcl_ecg_mian_history.setNoMore(true);
                        isCanLoadmore = false;
                    }else{
                        rcl_ecg_mian_history.setNoMore(false);
                        isCanLoadmore = true;
                    }
                }else{
                    View emptyView = LayoutInflater.from(this).inflate(R.layout.empty_nodata,null);
                    TextView tv_empty = (TextView) emptyView.findViewById(R.id.tv_empty);
                    tv_empty.setText("暂无历史记录");
                    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
                    params.width = DensityUtil.getScreenIntWidth(this);
                    params.height = DensityUtil.getScreenIntHeight(this) - DensityUtil.sp2px(this,50);
                    emptyView.setLayoutParams(params);
                    ((ViewGroup)rcl_ecg_mian_history.getParent()).addView(emptyView);
                    rcl_ecg_mian_history.setEmptyView(emptyView);
                    frl_leftview.setVisibility(View.GONE);
                }

                break;
            case  Act.Flag.FLAG_LOAD_MODE_DATA:////上拉加载更多
        }
    }
    private void loadData(String flagRefreshData) {
       //provider.requestDataDetails(deviceType,flagRefreshData);
       provider.requestGetFamilyMemberItemData2(touserid, deviceType, "", String.valueOf(pageNo), String.valueOf(pageSize), flagRefreshData);
    }
}
